package controllers;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;

import model.TeremnyilvantartasModel;

import org.eclipse.swt.SWT;
import org.eclipse.swt.events.MouseAdapter;
import org.eclipse.swt.events.MouseEvent;
import org.eclipse.swt.events.PaintEvent;
import org.eclipse.swt.events.PaintListener;
import org.eclipse.swt.graphics.Rectangle;

import views.FoglalasOnUiView;
import views.TeremOnUiView;
import beans.Foglalas;
import beans.Terem;
import dialogs.FoglalasKarbanTartasDialog;

public class TeremOnUIController {

	private TeremOnUiView view;
	private Terem terem;
	private Calendar datum;
	private List<Foglalas> foglalasok;
	private Map<FoglalasOnUiView, PaintListener> foglalasOnUiMap;
	private TeremNyilvantartasMainController mainController;
	private TeremnyilvantartasModel model;
	private SimpleDateFormat sdf = new SimpleDateFormat("YYYY.MM.dd EEEEE");

	public TeremOnUIController(TeremOnUiView view, Terem terem, Calendar datum,
			List<Foglalas> foglalasok,
			TeremNyilvantartasMainController mainController,
			TeremnyilvantartasModel model) {
		this.view = view;
		this.terem = terem;
		this.datum = datum;
		this.foglalasok = foglalasok;
		this.mainController = mainController;
		this.model = model;
		this.foglalasOnUiMap = new HashMap<FoglalasOnUiView, PaintListener>();
		setLabels();
		refreshFoglalasOnUi();
		addListeners();
		view.layout();
	}

	private void setLabels() {
		view.getLblTeremnv().setText(terem.getTeremNev());
		view.getLblDtum().setText(sdf.format(datum.getTime()));
	}

	private void addListeners() {
		view.getIdoVonal().addMouseListener(new MouseAdapter() {

			@Override
			public void mouseDoubleClick(MouseEvent arg0) {
				FoglalasKarbanTartasDialog dialog = new FoglalasKarbanTartasDialog(
						view.getShell(), SWT.NONE);
				Foglalas createdFoglalas = dialog.open(terem, datum,
						AblakTipus.INSERT, null, dialog.getView(), model);
				if (createdFoglalas != null) {
					createdFoglalas.setTeremId(createdFoglalas.getTerem()
							.getId());
					createdFoglalas.setFoglaloId(createdFoglalas.getFoglalo()
							.getId());
					createdFoglalas.setTipusId(createdFoglalas.getTipus()
							.getId());
					model.getFoglalasok().add(createdFoglalas);
					mainController.redrawAll();
				}
			}

		});
	}

	public void refreshFoglalasOnUi() {
		for (Entry<FoglalasOnUiView, PaintListener> viewToRemove : foglalasOnUiMap
				.entrySet()) {
			if (viewToRemove.getKey() != null
					&& !viewToRemove.getKey().isDisposed()) {
				viewToRemove.getKey().removePaintListener(
						viewToRemove.getValue());
				viewToRemove.getKey().dispose();
			}
		}
		foglalasOnUiMap.clear();
		if (foglalasok != null) {
			for (final Foglalas foglalas : foglalasok) {
				final FoglalasOnUiView foglalasOnUiView = new FoglalasOnUiView(
						view.getIdoVonal(), SWT.BORDER);
				new FoglalasOnUiController(foglalasOnUiView, foglalas, this,
						this.model);
				PaintListener currentListener = new PaintListener() {

					@Override
					public void paintControl(PaintEvent e) {
						if (!foglalasOnUiView.isDisposed()) {
							Rectangle clientArea = view.getIdoVonal()
									.getClientArea();
							final float startPointY = ((foglalas.getFromDate()
									.get(Calendar.HOUR_OF_DAY) - 6))
									+ (foglalas.getFromDate().get(
											Calendar.MINUTE) / 60f);
							final float endPointY = ((foglalas.getToDate().get(
									Calendar.HOUR_OF_DAY) - 6))
									+ (foglalas.getToDate()
											.get(Calendar.MINUTE) / 60f);
							int startPointX = (int) Math.round(clientArea.width
									* new Double("0.35"));
							float egyseg = (clientArea.height / 14f);
							int realStartPointY = Math.round(startPointY
									* egyseg);
							int realEndPointY = Math
									.round((endPointY * egyseg));
							Rectangle pos = new Rectangle(startPointX,
									realStartPointY, clientArea.width
											- startPointX - 5, realEndPointY
											- realStartPointY);

							foglalasOnUiView.setBounds(pos);
						}
					}

				};
				view.getIdoVonal().addPaintListener(currentListener);
				foglalasOnUiMap.put(foglalasOnUiView, currentListener);
			}
		}
	}

	public List<Foglalas> getFoglalasok() {
		return foglalasok;
	}

	public TeremNyilvantartasMainController getMainController() {
		return mainController;
	}

	public void removeTeremOnUi() {
		view.dispose();
	}
}
